import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

import lejos.nxt.*;
/**
 * The locomotions of a  LEGO 9797 car is controlled by
 * sound detected through a microphone on port 1. 
 * 
 * @author  Ole Caprani
 * @version 23.08.07
 */
public class ClapCtrCar 
{
	private static int noiseLevel = 0;
    private static int upperSoundThreshold = 65;
    private static int lowerSoundThreshold = 50;
    private static SoundSensor sound = new SoundSensor(SensorPort.S2);
    private final static int recordingArraySize = 10;
    private static DataLogger logger;
	
    private static void waitForClap() throws Exception
    {
        boolean clap = false;
        int[] soundRecordings = new int[recordingArraySize];
        int counter = 0;
        
        /*if(noiseLevel > soundThreshold)
        { //skriv noget
        	System.exit(0);
        }*/
        
        for(int i = 0; i<recordingArraySize; i++)
        {
        	soundRecordings[i] = noiseLevel;
        }
        LCD.drawInt(noiseLevel, 4, 0, 4);
        do
        {
            soundRecordings[counter%recordingArraySize] = sound.readValue();
            
            LCD.drawInt(soundRecordings[(counter)%recordingArraySize], 4, 0, 3);
            
            /*Thread.sleep(500);*/
            
            LCD.drawString("Left",0,1);
            
            //int maxVolume = 0;
            /*for(int i = 0; i< recordingArraySize; i++)
            {
            	if(soundRecordings[i] > maxVolume)
            		maxVolume = soundRecordings[i];
            }*/
            
            
            if(soundRecordings[(counter)%recordingArraySize] > upperSoundThreshold)
            {
            	long startTime = System.currentTimeMillis();
            	while(System.currentTimeMillis() - startTime < 250)
            	{
            		soundRecordings[counter%recordingArraySize] = sound.readValue();
            		if(soundRecordings[counter%recordingArraySize]< lowerSoundThreshold){
            			            			
            			/*for(int i = 0; i < recordingArraySize; i++)
            			{
            				logger.start();
            				logger.writeSample(soundRecordings[(i+counter)%recordingArraySize]);
            				logger.close();
            			}*/
            			
	            		clap = true;
            		}
            		counter++;
            	}
            }
            counter++;
            
            if(counter == 1000)
            	counter = 0;
            
        }
        while ( !clap );
    }
    
    private static void calibrate() throws InterruptedException
    {
    	int size = 100;
    	int[] recordings = new int[size];
    	for(int i = 0; i< size; i++)
    	{
    		Thread.sleep(10);
    		recordings[i] = sound.readValue();
    	}
    	Arrays.sort(recordings);
    	noiseLevel = recordings[size/2];
    }

    public static void main(String [] args) throws Exception
    {
    	logger = new DataLogger("Clapping_data.csv", false);
    	
    	Button.ESCAPE.addButtonListener(new ButtonListener() {
			
			@Override
			public void buttonReleased(Button b) {

			       Car.stop();
			       LCD.clear();
			       System.exit(0);				
			}
			
			@Override
			public void buttonPressed(Button b) {
				// TODO Auto-generated method stub
				
			}
		});
    	
    	calibrate();
    	
        LCD.drawString("dB level: ",0,0);
        LCD.refresh();
	   	   
        while (true)
        {
            waitForClap();		    			   
            LCD.drawString("Forward",0,1);
           	Car.forward(100, 100);
		    
            waitForClap();		    			   
            LCD.drawString("Right   ",0,1);
            Car.forward(100, 0);
		    
            waitForClap();		    			   
            LCD.drawString("Left    ",0,1);
            Car.forward(0, 100);
		    
            waitForClap();		    			   
            LCD.drawString("Stop    ",0,1); 
            Car.stop();
       }
   }
}
